<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>分类添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    @include('layout.link')
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="layui-container fly-marginTop">
    <div class="fly-panel" pad20 style="padding-top: 5px;">
        <div class="layui-form layui-form-pane">
            <div class="layui-tab layui-tab-brief" lay-filter="user">
                <ul class="layui-tab-title">
                    <li class="layui-this">发表</li></ul>
                <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                    <div class="layui-tab-item layui-show">
                        <form method="post">
                            <div class="layui-row layui-col-space15 layui-form-item">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">所在分类</label>
                                    <div class="layui-input-block">
                                        <select lay-verify="required" name="cate_id" lay-filter="column">
                                            <option></option>
                                            @foreach($cates as $cate)
                                                <option value="{{$cate->id}}" @if($cate->id == $article->cate_id) selected @endif >{{$cate->name}}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-col-md9">
                                    <label for="L_title" class="layui-form-label">标题</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="L_title" value="{{$article->title}}" name="title" required lay-verify="required" autocomplete="off" class="layui-input"></div>
                                </div>

                                <div class="layui-col-md9">
                                    <button  type="button" class="layui-btn"   id="test1">
                                        <i class="layui-icon">&#xe67c;</i>文章图片
                                    </button>
                                </div>
                                <div class="layui-col-md9">
                                    <label for="L_title" class="layui-form-label">标签</label>
                                    <div class="layui-input-block" >
                                        @foreach($tags as $tag)
                                            <input name="tag_id[{{$tag->id}}]" type="checkbox"  @if($article->tags->contains($tag)) checked  @endif value="{{$tag->id}}"> {{$tag->name}}
                                        @endforeach
                                    </div>
                                </div>


                            </div>
                            <div class="layui-form-item layui-form-text">
                                <div class="layui-input-block">
                                    <textarea id="L_content" name="content"  placeholder="详细描述" class="layui-textarea fly-editor" style="height: 260px;">{{$article->content}}</textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-filter="addArt" lay-submit>立即发布</button></div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','jquery','layedit'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var layedit = layui.layedit;

        layedit.set({
            uploadImage: {
                url: "{{url('/article/upload')}}" //接口url
                ,type: 'post'
            }
        });
        thearea = layedit.build('L_content'); //建立编辑器
        //监听提交
        form.on('submit(addArt)', function(data){
            data.field.content = layedit.getContent(thearea)
            $.ajax({
                url:"/article/{{$article->id}}",
                type: 'PUT',
                data:data.field,
                dataType: 'json',
                success: function (data) {
                    if(data.error ==1){
                        layer.msg(data.info, function(){
                        });
                    }else if(data.error ==0){
                        layer.alert(data.info, {icon: 6},function(){
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            // 关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                },
                fail: function (err) {
                    console.log(err)
                },
                statusCode: {
                    422: function(msg) {
                        var errors = jQuery.parseJSON(msg.responseText).errors;
                        var error = '';
                        for (error in errors){

                        }
                        // console.log()
                        layer.msg(errors[error][0], function(){
                        });
                    },
                    401: function(data) {
                        layer.msg('请先登录', function(){
                        });
                    }
                }

            });
            return false;
        });
    });
</script>

<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/articleImg'//上传接口

            ,done: function(res){
                //上传完毕回调
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>

</body>
</html>